package de.ifgi;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BtReceiver extends BroadcastReceiver {

	private static final String tag = "OIL";
	private long startTime;
	private long stopTime;
	private NodesList nodesList;
	private EventsList eventsList;

	
	@Override
	public void onReceive(Context ctx, Intent intent) {
		String strIntent = intent.getAction();
		if (strIntent.equalsIgnoreCase(BluetoothDevice.ACTION_FOUND)) {
			handleFoundResult(ctx, intent);
		} else if (strIntent.equalsIgnoreCase(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
			handleDiscStarted(ctx, intent);
		} else if (strIntent.equalsIgnoreCase(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
			handleDiscFinished(ctx, intent);
		} else if (strIntent.equalsIgnoreCase("de.ifgi.oil.action.NODES_TO_RECEIVER")) {
			sendNodesToActivity(ctx, intent);
		}

	}

	private void sendNodesToActivity(Context ctx, Intent intent) {
		Point userPos = intent.getParcelableExtra("userPos");		
//		Log.d(tag, "onBtReceiver="+userPos.getX()+","+userPos.getY());
		Intent sendNodesInt = new Intent("de.ifgi.oil.action.NODES_READY");
		sendNodesInt.putExtra("userPos", userPos);
		ctx.sendBroadcast(sendNodesInt);
	}

	private void handleDiscFinished(Context ctx, Intent intent) {
		stopTime = System.currentTimeMillis();
		Log.d(tag, "\nDiscovery finished " + stopTime);
		// TODO: startTime ga kebaca??
//		Log.d(tag, "\nstopTime=" + stopTime + ", startTime=" + startTime);
		// collect data for emergency events
//		Intent eventsInt = new Intent("de.ifgi.oil.action.EVENTS_READY");
//		ctx.sendBroadcast(eventsInt);
	}

	private void handleDiscStarted(Context ctx, Intent intent) {
		startTime = System.currentTimeMillis();
		Log.d(tag, "\nDiscovery started " + startTime);
		
		// when new discovery is started, set nodesList and eventsList to empty
		nodesList = NodesList.getNodesList();
		nodesList.clear();
        nodesList.setUsed(false); // never been used for localization
		eventsList = EventsList.getEventsList();
		eventsList.clear();
		
		 // ONLY FOR TESTING!!	
//		Node n1 = new Node("Node1", "-48", new Point(404484.52,5755415.818));
//		Node n2 = new Node("Node2", "-53", new Point(404480.238,5755415.818));
//		Node n3 = new Node("Node3", "-48", new Point(404482.775,5755412.25));
//		
//		nodesList.add(n1);
//		nodesList.add(n2);
//		nodesList.add(n3);
//		
//		Intent serviceIntent = new Intent(ctx, Localization.class);
//    	ctx.startService(serviceIntent);
	}

	private void handleFoundResult(Context ctx, Intent intent) {
//		nodesList = NodesList.getNodesList();
//		eventsList = EventsList.getEventsList();
//		
//		BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//		if (device != null) {
//			
//			String btFullName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
//			if (btFullName.startsWith("Node")) {
//				
//				String[] nodeFullName = btFullName.split("/");
//				
//				String btName = nodeFullName[0];
//				String estPosition = nodeFullName[1];
//				double x = Double.parseDouble(estPosition.split(",")[0]);
//				double y = Double.parseDouble(estPosition.split(",")[1]);
//				Point pos = new Point(x, y);
//				Short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
//				
//				Node node = new Node(btName, rssi.toString(), pos);
//				
//				if (nodesList.hasNode(node)) {
//					nodesList.updateNode(node);
//				} else {
//					nodesList.add(node);
//				}
//				
//				if (nodesList.size() == 3 && !nodesList.isUsed()) {
//					nodesList.setUsed(true);
//					Intent nodesInt = new Intent(ctx, Localization.class);
//			    	ctx.startService(nodesInt);
//				}
//				
//			}
//			
//			if (btFullName.startsWith("Event")) {
//				String[] nodeFullName = btFullName.split("/");
//				
//				String evtType = nodeFullName[0];
//				String evtLevel = nodeFullName[1];				
//				String estPosition = nodeFullName[2];
//				double x = Double.parseDouble(estPosition.split(",")[0]);
//				double y = Double.parseDouble(estPosition.split(",")[1]);
//				Point pos = new Point(x, y);
//				
//				Event event = new Event(evtType, evtLevel, pos);
//				if (eventsList.hasEvent(event)) {
//					eventsList.updateEvent(event);
//				} else {
//					eventsList.add(event);
//				}
//			}
//			
//		}
	}

}
